#ifndef MSTIME_H
#define MSTIME_H
#include <libmslog/mstype.h>

typedef struct RSTime{
	ms_u32 tmsec;		/* Seconds: 0-59 (K&R says 0-61?) */
	ms_u32 tmmin;		/* Minutes: 0-59 */
	ms_u32 tmhour;		/* Hours since midnight: 0-23 */
	ms_u32 tmday;		/* Day of the month: 1-31 */
	ms_u32 tmmon;		/* Months *since* january: 0-11 */
	ms_u32 tmyear;		/* Years since 1900 */
	ms_u32 tmwday;
}RSTime,* PRSTime;


/*****************************************************
* :Time
*****************************************************/

#define ms_seconds(x) 			(x)
#define ms_minutes(x)			(x*60*ms_seconds(1))
#define ms_hours(x)			(x*60*ms_minutes(1))
#define ms_days(x)				(x*24*ms_hours(1))


#define ms_mseconds(x)		(x)
#define ms_msseconds(x) 		(x*1000*ms_mseconds(1))
#define ms_msminutes(x)		(x*60*ms_msseconds(1))
#define ms_mshours(x)			(x*60*ms_msminutes(1))
#define ms_msdays(x)			(x*24*ms_mshours(1))

#define ms_useconds(x)			(x)
#define ms_usmseconds(x)		(x*1000*ms_useconds(1))
#define ms_usseconds(x) 		(x*1000*ms_usmseconds(1))
#define ms_usminutes(x)		(x*60*ms_usseconds(1))
#define ms_ushours(x)			(x*60*ms_usminutes(1))
#define ms_usdays(x)			(x*24*ms_ushours(1))

#define mstime_fmt_date 			"%04d-%02d-%02d"
#define mstime_fmt_ym 			"%04d%02d"
#define mstime_fmt_time 			"%02d:%02d:%02d"
#define mstime_fmt_time_ex1 		"%02d%02d00"
#define mstime_fmt_time_ex2 		"%02d0000"
#define mstime_fmt_time_ex3 		"%02d3000"
#define mstime_fmt_datetime 		"%04d-%02d-%02d %02d:%02d:%02d"
#define mstime_fmt_datetime_ex1 	"%04d/%02d/%02d %02d:%02d:%02d"
#define mstime_fmt_datetime_ex2 	"%04d%02d%02d-%02d:%02d:%02d"
#define mstime_fmt_datetime_ex3 	"%04d%02d%02d%02d%02d%02d"
#define mstime_fmt_datetime_ex4 	"%04d/%02d/%02d-%02d:%02d:%02d"

#ifndef MSTIME_C
extern ms_void 	//B20150603123025E20150603123025
	mstime_api_parsetimestr1(ms_string ms_in pstr, PRSTime ms_out ptm_start, 
	PRSTime ms_out ptm_stop,ms_ps08 ms_out outbuf);	
extern ms_void 	//2015-06-03 12:30:25
	mstime_api_parsetimestr2(ms_string ms_in pstr, PRSTime ms_out prstime);	
extern ms_void 	//20150603-12:30:25
	mstime_api_parsetimestr3(ms_string ms_in pstr, PRSTime ms_out prstime);	
extern ms_void 
	mstime_api_rstimePrint(PRSTime ms_io prstime);
extern ms_u32 
	mstime_api_rstimeGetSec(RSTime * ms_out prstime);
extern ms_s64 
	mstime_api_rstimeMktime(PRSTime ms_in pTm);	//s
extern ms_void 
	mstime_api_rstimeGetCur(RSTime * ms_out prstime);
extern ms_u64 
	mstime_api_us(ms_void);
extern ms_u64 
	mstime_api_ms(ms_void);
extern ms_u64 
	mstime_api_sec(ms_void);
extern ms_u32 
	mstime_api_minute(ms_void);
extern ms_u32 
	mstime_api_hour(ms_void);
extern ms_s08 	   
	mstime_api_mouth(ms_void);
extern ms_bool 
	mstime_api_setDTime(char *datetime_string);
extern ms_string 
	mstime_api_curDate(ms_string ms_in frm,ms_string ms_out str_curDate);
extern ms_string 
	mstime_api_curYM(ms_string ms_in frm,ms_string ms_out str_curYM);
extern ms_string 
	mstime_api_curTime(ms_string ms_in frm,ms_string ms_out str_curtime);
extern ms_string 
	mstime_api_curDTime(ms_string ms_in frm,ms_string ms_out strCurDTime);
extern ms_void    
	mstime_api_us2DTime(ms_u64 * ptimeus,ms_string ms_in frm,char * strtime);
extern ms_void    
	mstime_api_sec2DTime(ms_u64 * ptimesec,ms_string ms_in frm,char * strtime);
extern ms_void 
	mstime_api_us2str(ms_u64 * ptimeus,ms_string ms_in frm,char * strtime);
extern ms_void 
	mstime_api_sec2str(ms_u64 * ptimesec,ms_string ms_in frm,char * strtime);
extern ms_string 
	mstime_api_rtstime2DTime(ms_string ms_in fmt,RSTime ms_in rstime,ms_string outstr);
extern ms_string 
	mstime_api_sec2str2(ms_u32 ms_in timesec,ms_string outbuf);
extern ms_string 
	mstime_api_ms2str2(ms_u64 ms_in timeMs,ms_string outbuf);
extern ms_string 
	mstime_api_us2str2(ms_s64 ms_in timesec,ms_string outbuf);
extern ms_u64 
	mstime_api_upTimeUs(ms_void);
extern ms_u64 
	mstime_api_upTimeMs(ms_void);
extern ms_u64 
	mstime_api_upTimeSec(ms_void);
extern ms_bool 
	mstime_api_isNewDay(time_t * ms_io timep_pre)	;
extern ms_bool 
	mstime_api_isNewLoop(ms_string ms_in pstr,ms_s32 timeout,ms_bool *flag_cur,ms_bool *flag_pre);	//s
extern ms_bool 
	mstime_api_isWholeClock(time_t *ms_io timep_pre);
extern ms_bool  
	mstime_api_isVaildDate( RSTime ms_in rstime_now,ms_string ms_in start_date,ms_string ms_in end_date);
extern ms_bool 
	mstime_api_isVaildTime( RSTime ms_in rstime_now,ms_string ms_in start_time,ms_string ms_in end_time);
extern ms_bool 
	mstime_api_isVaildWDay(RSTime ms_in rstime_now, ms_s32 ms_in vaildwday);
extern ms_bool 
	mstime_api_isPass(PRSTime ms_in pTm);
extern ms_bool 
	mstime_api_counterAsyncSec(time_t * ms_in Etimep,ms_u32 ms_in sec);	//s
extern ms_bool 
	mstime_api_counterAsyncUs(ms_u64 * ms_in Etimep,ms_u64 ms_in time_us);	//us
extern ms_bool 
	mstime_api_moreThan(PRSTime ms_in pTm1,PRSTime ms_in pTm2);
extern ms_bool 
	mstime_api_timeout(PRSTime ms_in ptm_start,PRSTime ms_in ptm_stop,ms_u64 timeou_sec);	//s
extern ms_bool 
	mstime_api_timeout2(ms_u64 time1,ms_u64 time2,ms_u64 timeout);	
extern ms_bool 
	mstime_api_timeout3(PRSTime ms_in ptm_start,PRSTime ms_in ptm_stop,ms_u64 timeou_sec);//s
extern ms_bool 
	mstime_api_disorder(ms_u64 *cur_time,ms_u64 *pass_time,ms_u64 timeout_us);
extern ms_void 
	mstime_api_cast(ms_u64 *preTime_us,ms_string light,ms_cstring ms_in func,ms_u32 ms_in line);

#define mstime_cast(preTime_us)					mstime_api_cast(preTime_us,ms_null,__FUNCTION__,__LINE__);
#define mstime_castLight(preTime_us,light)		mstime_api_cast(preTime_us,light,__FUNCTION__,__LINE__);
#else
ms_string 
	mstime_api_sec2str2(ms_u32 ms_in timesec,ms_string outbuf);
#endif

#endif
